package com.loopy.utils;

import org.testng.annotations.Test;

import java.sql.*;

public class JDBCUtils_Use {
    @Test
    public void testSelect(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        String sql="select * from actor";
        try {
            connection=jdbcUtils.getConnection();
            preparedStatement= connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("NAME");
                String sex = resultSet.getString("sex");
                String phone = resultSet.getString("phone");
                Date borndate = resultSet.getDate("borndate");
                System.out.println(id+"\t"+name+"\t"+sex+"\t"+phone+"\t"+borndate);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            jdbcUtils.close(resultSet,preparedStatement,connection);
        }
    }
    @Test
    public void testDML() {
        Connection connection = null;
        String sql = "update actor set name= ? where id = ?";
        PreparedStatement preparedStatement = null;
        try {
            connection = jdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "刘德华");
            preparedStatement.setInt(2, 4);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            jdbcUtils.close(null, preparedStatement, connection);
        }

    }
}
